package game;
/*
Copyright (C) 2012 Johan Ceuppens

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/


import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

import java.awt.Frame;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;
import javax.media.opengl.glu.GLU;
import java.util.*;
import java.io.*;
import javablender.*;

public class Game implements GLEventListener,KeyListener {

	GLAutoDrawable drawabletemp;

	private int fadingcounter = 0;
	Font titlescreenfont = new Font("Helvetica", Font.BOLD, 25);
	Font titlescreenfont2 = new Font("Helvetica", Font.BOLD, 15);
	private int levelnumber = 1000;

    private float theta = (float)0.0;
    private float fraci = (float)0.0;
    private float s = 0;
    private float c = 0;
	private String configprefix = "./config/";
	private String prefix = "./pics/";
	Blender blenderfilereader = new Blender();
	
	private LinkedList terrainpieces = new LinkedList();;
	private LinkedList modelpieces = new LinkedList();;
	private LinkedList bullets = new LinkedList();;
static	private int[] xarray;
static	private int[] yarray;
static	private int[] zarray;
static	private int[] carray;
	private boolean showtitlescreen = false; 
	private int update = 0;//integer value which update should be performed (e.g. through keyboard, see KeyAdapter) 
	int width,height;
	private float dz = 0.0f;
	private BufferedImage imgImg;
	private BufferedImage bufferedImage;
	WritableRaster raster;
        ByteBuffer bb;
    public static void main(String[] args) {
	System.out.println("Loading..please wait until the window opens...");
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);

/*
	public class JoglEventListener implements GLEventListener, KeyListener {
		private GLCanvas = canvas;

		public JoglEventListener(GLCanvas canvas) {
			this.canvas = canvas;
		}
	}

	JoglEventListener listener = new JoglEventListener(canvas);
	canvas.addGLEventListener(listener);
	canvas.addKeyListener(listener);
*/
	Frame frame = new Frame("Space Odyssey 3D");
        frame.setSize(320, 200);
        frame.add(canvas);
        frame.setVisible(true);
	
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
	Game game = new Game();
        canvas.addGLEventListener(game);
	canvas.addKeyListener(game);
        Object o = new Animator(canvas);
        Animator animator = (Animator)o;
        animator.add(canvas);
        animator.start();
    }

	public void loadLevel1000() {
	        GL2 gl = drawabletemp.getGL().getGL2();
		TerrainGen tp = new TerrainPieceWalkingTube(320,200,4,GL2.GL_LINES);
		TerrainGen tp2 = new TerrainPiecePillar(0,0,0,63,100,4,GL2.GL_POINTS);
		terrainpieces.add(tp);
		terrainpieces.add(tp2);

		//ModelGen mg = new 


	}

/*
 * Game configuration from config file
 */

	public void loadConfiguration(String filename)
	{

		LinkedList configlst = new LinkedList();
		try {
			FileReader input  = new FileReader(filename);

			BufferedReader bufread = new BufferedReader(input);
	
			String line  = bufread.readLine();
		
			while (line != null) {
				line+="\n";
				configlst.add(line);
				line = bufread.readLine();	
			}

			bufread.close();

		} catch (IOException e) {

		}

		for (int i = 0; i < configlst.size(); i++) {
			Object o = configlst.get(i);
			String s = (String)o;
			if (setConfiguration(s) < 0) {
				System.out.println("Bad configuration file line : " + configlst.get(i));
				return;	

			}	
		}
	}

	public int setConfiguration(String configline)
	{

		int idx1 = configline.indexOf(' ');	
		int idx2 = configline.indexOf('\n');	
		if (idx1 >= configline.length() || idx2 < configline.length()-1)
			return -1;	

		String s = configline.substring(0,idx1);
		String s2 = configline.substring(idx1+1,configline.length()-1); 

		if (s2 == "")
			return -1;

		int s2i = Integer.parseInt(s2);//skip newline
			
		switch(s){
		case "bigendian":
			break;
		}


		return 0;
	}

	public void loadBlendFile(String filename) {
		blenderfilereader.load(prefix+filename);	
		xarray = blenderfilereader.getxarray();	
		yarray = blenderfilereader.getyarray();	
		zarray = blenderfilereader.getzarray();	
		carray = blenderfilereader.getcarray();
	}

	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
	        GL2 gl = drawabletemp.getGL().getGL2();
	        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

		if (showtitlescreen) {
			if (fadingcounter <= 20) {
				if (key == KeyEvent.VK_X || key == KeyEvent.VK_Z) {
		        	for (int i = 0; i < 30; i++) {
					gl.glClear(GL.GL_COLOR_BUFFER_BIT);
					display(drawabletemp);
					fadingcounter++;
				}
			}	
			else {
				showtitlescreen = false;
				fadingcounter = 0;
			}
			return;
			}
		}
		if (key == KeyEvent.VK_LEFT) {
			System.out.println("LEFT key pressed - rotate");
			update = 1;
       			doupdateleft();
        		render(drawabletemp);
			
		} else if (key == KeyEvent.VK_RIGHT) {
			System.out.println("RIGHT key pressed - rotate");
			update = 1;
       			doupdateright();
        		render(drawabletemp);
		} else if (key == KeyEvent.VK_UP) {
			System.out.println("UP key pressed - translate forward on Z axis");
			update = 2;
        		render(drawabletemp);
		} else if (key == KeyEvent.VK_DOWN) {
			System.out.println("UP key pressed - translate back on Z axis");
			update = 3;
        		render(drawabletemp);
		} else if (key == KeyEvent.VK_X) {
			System.out.println("X key pressed - perspective");
			update = 4;
        		render(drawabletemp);
		} else if (key == KeyEvent.VK_Z) {
			System.out.println("Z key pressed ");
			BulletModel b = new BulletModel(320,200,4);
			bullets.add(b);
		
		} else {
			update = -1;
		}
	}

	public void keyReleased(KeyEvent e) {
		//System.out.println("key released");
	}

	public void keyTyped(KeyEvent e) {
		//System.out.println("key typed");
	}


    public void display(GLAutoDrawable drawable) {


	if (showtitlescreen) {
        		GL2 gl = drawable.getGL().getGL2();
                        Graphics2D g = imgImg.createGraphics();
                        g.drawImage(bufferedImage, null,null);
			g.setColor(Color.blue);
			g.setFont(titlescreenfont);
			g.drawString("Space Odyssey 3D", 20,165);
			g.setColor(Color.gray);
			g.setFont(titlescreenfont2);
			g.drawString("press X key to start", 20,185);
			bb.position(0);
			for (int i = 0; i < 320*240*4; i++) {
				byte b = bb.get(i); 
				if (i%4!=3 && fadingcounter != 0)  {
					b-=2;
					if (b < 0)
						b = 0;
				}	
				bb.put((byte)b);	
			}
                        bb.position(0);
                        //bb.mark();
                        int w = bufferedImage.getWidth();
                        int h = bufferedImage.getHeight();
                        gl.glBindTexture(GL2.GL_TEXTURE_2D, 13);
                        gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
                        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
                        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
                        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
                        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
                        gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
                        gl.glTexImage2D (GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, w, h, 0, GL2.GL_RGBA,
                                        GL2.GL_UNSIGNED_BYTE, bb);

                        int left = 0;//FIXME fixed
			int top = 0;
                        gl.glEnable(GL2.GL_TEXTURE_2D);
                        gl.glBindTexture (GL2.GL_TEXTURE_2D, 13);
                        gl.glBegin (GL2.GL_POLYGON);
                        gl.glTexCoord2d (0, 0);
                        gl.glVertex2d (left,top);
                        gl.glTexCoord2d(1,0);
                        gl.glVertex2d (left+w, top);
                        gl.glTexCoord2d(1,1);
                        gl.glVertex2d (left+w, top+h);
                        gl.glTexCoord2d(0,1);
                        gl.glVertex2d (left, top+h);
                        gl.glEnd ();
                        gl.glFlush();
    
			return; 
	}

        GL2 gl = drawable.getGL().getGL2();

	gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);
   	//doupdate();
	
	if (update < 0) 
		return;

        render(drawable);

	gl.glMatrixMode(GL2.GL_MODELVIEW);
	//NOTE translation must be + rotation methods here under
	gl.glLoadIdentity();
	if (update == 1) {
		gl.glRotatef(theta,2.0f,2.0f,5.0f);
	}
	if (update == 2) {
		dz += 0.1f;
		gl.glRotatef(theta,2.0f,2.0f,5.0f);
		gl.glTranslatef(0.0f,0.0f,dz);//move +1 on z-axis
	}
	if (update == 3) {
		dz -= 0.1f;
		gl.glRotatef(theta,2.0f,2.0f,5.0f);
		gl.glTranslatef(0.0f,0.0f,dz);//move -1 on z-axis
	}
	if (update == 4) {
		gl.glFrustum(-100.0f,100.0f,-100.0f,100.0f, 1.0f, 5.0f);
    	}

	for (int i = 0; i < bullets.size(); i++) {
		Object o = bullets.get(i);
		BulletModel b = (BulletModel)o;
  		b.move(); 
	}
	}


    public void dispose(GLAutoDrawable drawable) {
    }

    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
	drawabletemp = drawable;
	gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
	gl.glColor3f(0.0f,0.0f,200.0f);
	gl.glClearDepth(1.0f);
	gl.glEnable(GL2.GL_DEPTH_TEST);
	gl.glDepthFunc(GL2.GL_LEQUAL);
	gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT,GL2.GL_NICEST);
	gl.glShadeModel(GL2.GL_SMOOTH);

              gl.glMatrixMode(GL2.GL_PROJECTION);
                        gl.glLoadIdentity();
                        gl.glOrtho(0, 320, 0, 200, -1, 1);
                        gl.glMatrixMode(GL2.GL_MODELVIEW);
                        gl.glDisable(GL2.GL_DEPTH_TEST);
                        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
                        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
                        gl.glBlendFunc (GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
                        gl.glEnable (GL2.GL_BLEND);
                        bufferedImage = null;
                        int w = 0;
                        int h = 0;
                        try {
                                bufferedImage = ImageIO.read(new File(prefix+"titlescreen1.png"));
                                w = bufferedImage.getWidth();
                                h = bufferedImage.getHeight();
                        } catch (IOException e) {	
			}
			raster =
                                Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
                                                w,
                                                h,
                                                4,
                                                null);
                        ComponentColorModel colorModel=
                                new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                                new int[] {8,8,8,8},
                                                true,
                                                false,
                                                ComponentColorModel.TRANSLUCENT,
                                                DataBuffer.TYPE_BYTE);
                        imgImg =
                                new BufferedImage (colorModel,
                                                raster,
                                                false,
                                                null);

			DataBufferByte imgBuf =
                                (DataBufferByte)raster.getDataBuffer();
                        byte[] imgRGBA = imgBuf.getData();

                        bb = ByteBuffer.wrap(imgRGBA);
	//loadBlendFile("monkeys.blend");
	loadConfiguration(configprefix+"config.txt");
	loadLevel1000(); 
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
    }

    private void doupdateleft() {
	theta += 1.8f;
   	fraci += 0.32f; 
    }

    private void doupdateright() {
	theta -= 1.8f;
   	fraci -= 0.32f; 
    }


    public float normal(float xx1, float yy1, float zz1, float xx2, float yy2, float zz2)
    {

	return (float)Math.sqrt((float)xx1*xx2 + (float)yy1*yy2 + (float)zz1*zz2);

    } 

    private void render(GLAutoDrawable drawable) {
		if (levelnumber == 1000) {
			renderTerrainGen(drawable);
			renderModelGen(drawable);
		}
	}

    private void renderModel(GLAutoDrawable drawable) {
		renderArrays(drawable);
	}

    private void renderArrays(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);
	int j = 0;
	for (int i = 0; i < xarray.length; i+=3)
	{
		float xx1 = xarray[i];
		float yy1 = yarray[i];
		float zz1 = zarray[i];
		float xx2 = xarray[i+1];
		float yy2 = yarray[i+1];
		float zz2 = zarray[i+1];
		float xx3 = xarray[i+2];
		float yy3 = yarray[i+2];
		float zz3 = zarray[i+2];
		float cc1 = carray[j];
		float cc2 = carray[j];
		float cc3 = carray[j];
		j++;

		float dd1 = 0.0f;
		float dd2 = 0.0f;
		float dd3 = 0.0f;
	        gl.glColor3f(200, cc2, cc3);
	        gl.glBegin(GL.GL_TRIANGLES);
	        gl.glVertex3f(xx1,yy1,zz1);//FIXMES
	        gl.glVertex3f(xx2,yy2,zz2);
	        gl.glVertex3f(xx3,yy3,zz3);
	        gl.glEnd();
    	}
	System.out.println("123> Done drawing model.");
	}

    private void renderModelGen(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);

	for (int j = 0; j < modelpieces.size(); j++) {
		Object o = modelpieces.get(j); 
		ModelGen tp = (ModelGen)o;
 
	for (int i = 0; i < tp.size()-4; i+=tp.padsize()) {//NOTE! -4 for outofbounds 
		if (tp.padsize() == 4) {

			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float xx3 = tp.getvertexx(i+2);
			float yy3 = tp.getvertexy(i+2);
			float zz3 = tp.getvertexz(i+2);
			float xx4 = tp.getvertexx(i+3);
			float yy4 = tp.getvertexy(i+3);
			float zz4 = tp.getvertexz(i+3);
	
			//draw a polygon from the map vertices
		        gl.glBegin(GL2.GL_POLYGON);
		        //gl.glBegin(GL2.GL_LINES);
			gl.glColor4f(1,1,0,1);//gradients
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glColor4f(1,0,1,1);
		        gl.glVertex3f(xx2,yy2,zz2);
			gl.glColor4f(1,1,1,1);
		        gl.glVertex3f(xx3,yy3,zz3);
			gl.glColor4f(0,1,0,0);
		        gl.glVertex3f(xx4,yy4,zz4);
	        
			gl.glEnd();
		}
		else if (tp.padsize() == 3) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float xx3 = tp.getvertexx(i+2);
			float yy3 = tp.getvertexy(i+2);
			float zz3 = tp.getvertexz(i+2);
			float cc1 = 0;
			float cc2 = 0;
			float cc3 = 100;
		        //gl.glBegin(GL2.GL_POLYGON);
		        gl.glBegin(GL2.GL_LINES);
			gl.glColor4f(1,1,0,1);
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glColor4f(1,0,1,1);
		        gl.glVertex3f(xx2,yy2,zz2);
		        gl.glVertex3f(xx3,yy3,zz3);
			gl.glEnd();
		}	 
		else if (tp.padsize() == 2) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float cc1 = 0;
			float cc2 = 0;
			float cc3 = 100;
		        //gl.glBegin(GL2.GL_POLYGON);
		        gl.glBegin(GL2.GL_LINE_STRIP);
			gl.glColor4f(1,1,0,1);
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glColor4f(1,0,1,1);
		        gl.glVertex3f(xx2,yy2,zz2);
			gl.glEnd();
	       //gl.glEnd();
	}
		else if (tp.padsize() == 1) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float cc1 = 0;
			float cc2 = 0;
			float cc3 = 100;
		        gl.glBegin(GL2.GL_POLYGON);
		        //gl.glBegin(GL2.GL_LINES);
			gl.glColor4f(1,1,0,1);
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glEnd();
	       //gl.glEnd();
	}
	}
	}
/*** BLENDER file vertices loading
	int j = 0;
	for (int i = 0; i < xarray.length; i+=3)
	{
		float xx1 = xarray[i];
		float yy1 = yarray[i];
		float zz1 = zarray[i];
		float xx2 = xarray[i+1];
		float yy2 = yarray[i+1];
		float zz2 = zarray[i+1];
		float xx3 = xarray[i+2];
		float yy3 = yarray[i+2];
		float zz3 = zarray[i+2];
		float cc1 = carray[j];
		float cc2 = carray[j];
		float cc3 = carray[j];
		j++;

		float dd1 = 0.0f;
		float dd2 = 0.0f;
		float dd3 = 0.0f;
	        gl.glColor3f(200, cc2, cc3);
	        gl.glBegin(GL.GL_TRIANGLES);
	        gl.glVertex3f(xx1/1000,yy1/1000,zz1/1000);//FIXMES
	        gl.glVertex3f(xx2/1000,yy2/1000,zz2/1000);
	        gl.glVertex3f(xx3/1000,yy3/1000,zz3/1000);
	        gl.glEnd();
    	}
***/
	//gl.glMatrixMode(GL2.GL_MODELVIEW);
	//gl.glFrustum(-100.0f,100.0f,-100.0f,100.0f, 1.0f, 5.0f);//FIXME remove
	//System.out.println("123> Done drawing terrain.");
	}
    private void renderTerrainGen(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);

	for (int j = 0; j < terrainpieces.size(); j++) {
		Object o = terrainpieces.get(j); 
		TerrainPieceBase tp = (TerrainPieceBase)o;
 
	for (int i = 0; i < tp.size()-4; i+=tp.padsize()) {//NOTE! -4 for outofbounds
		if (tp.padsize() == 4) {

			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float xx3 = tp.getvertexx(i+2);
			float yy3 = tp.getvertexy(i+2);
			float zz3 = tp.getvertexz(i+2);
			float xx4 = tp.getvertexx(i+3);
			float yy4 = tp.getvertexy(i+3);
			float zz4 = tp.getvertexz(i+3);
			float cc1 = tp.getvertexc(i);
			float cc2 = tp.getvertexc(i+1);
			float cc3 = tp.getvertexc(i+2);
			float cc4 = tp.getvertexc(i+3);

			//draw a polygon from the map vertices
		        gl.glBegin(tp.getGLdraw());
			gl.glColor3f((int)cc1<<24,(int)cc1<<16,(int)cc1<<8);
			//gl.glColor4f(1,1,0,1);//gradients
		        gl.glVertex3f(xx1,yy1,zz1);
			//gl.glColor4f(1,0,1,1);
			gl.glColor3f((int)cc2<<24,(int)cc2<<16,(int)cc2<<8);
		        gl.glVertex3f(xx2,yy2,zz2);
			//gl.glColor4f(1,1,1,1);
			gl.glColor3f((int)cc3<<24,(int)cc3<<16,(int)cc3<<8);
		        gl.glVertex3f(xx3,yy3,zz3);
			//gl.glColor4f(0,1,0,0);
			gl.glColor3f((int)cc4<<24,(int)cc4<<16,(int)cc4<<8);
		        gl.glVertex3f(xx4,yy4,zz4);
	        
			gl.glEnd();
		}
		else if (tp.padsize() == 3) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float xx3 = tp.getvertexx(i+2);
			float yy3 = tp.getvertexy(i+2);
			float zz3 = tp.getvertexz(i+2);
			float cc1 = tp.getvertexc(i);
			float cc2 = tp.getvertexc(i+1);
			float cc3 = tp.getvertexc(i+2);
		        gl.glBegin(tp.getGLdraw());
			gl.glColor3f((int)cc1<<24,(int)cc1<<16,(int)cc1<<8);
			//gl.glColor4f(1,1,0,1);
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glColor3f((int)cc2<<24,(int)cc2<<16,(int)cc2<<8);
			//gl.glColor4f(1,0,1,1);
		        gl.glVertex3f(xx2,yy2,zz2);
			gl.glColor3f((int)cc3<<24,(int)cc3<<16,(int)cc3<<8);
		        gl.glVertex3f(xx3,yy3,zz3);
			gl.glEnd();
		}	 
		else if (tp.padsize() == 2) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float xx2 = tp.getvertexx(i+1);
			float yy2 = tp.getvertexy(i+1);
			float zz2 = tp.getvertexz(i+1);
			float cc1 = tp.getvertexc(i);
			float cc2 = tp.getvertexc(i+1);
		        gl.glBegin(tp.getGLdraw());
			//gl.glColor4f(1,1,0,1);
			gl.glColor3f((int)cc1<<24,(int)cc1<<16,(int)cc1<<8);
		        gl.glVertex3f(xx1,yy1,zz1);
			//gl.glColor4f(1,0,1,1);
			gl.glColor3f((int)cc1<<24,(int)cc1<<16,(int)cc1<<8);
		        gl.glVertex3f(xx2,yy2,zz2);
			gl.glEnd();
	}
		else if (tp.padsize() == 1) {
			float xx1 = tp.getvertexx(i);
			float yy1 = tp.getvertexy(i);
			float zz1 = tp.getvertexz(i);
			float cc1 = tp.getvertexc(i);
		        gl.glBegin(tp.getGLdraw());
			//gl.glColor4f(1,1,0,1);
			gl.glColor3f((int)cc1<<24,(int)cc1<<16,(int)cc1<<8);
		        gl.glVertex3f(xx1,yy1,zz1);
			gl.glEnd();
	}
	}
	}
/*** BLENDER file vertices loading
	int j = 0;
	for (int i = 0; i < xarray.length; i+=3)
	{
		float xx1 = xarray[i];
		float yy1 = yarray[i];
		float zz1 = zarray[i];
		float xx2 = xarray[i+1];
		float yy2 = yarray[i+1];
		float zz2 = zarray[i+1];
		float xx3 = xarray[i+2];
		float yy3 = yarray[i+2];
		float zz3 = zarray[i+2];
		float cc1 = carray[j];
		float cc2 = carray[j];
		float cc3 = carray[j];
		j++;

		float dd1 = 0.0f;
		float dd2 = 0.0f;
		float dd3 = 0.0f;
	        gl.glColor3f(200, cc2, cc3);
	        gl.glBegin(GL.GL_TRIANGLES);
	        gl.glVertex3f(xx1/1000,yy1/1000,zz1/1000);//FIXMES
	        gl.glVertex3f(xx2/1000,yy2/1000,zz2/1000);
	        gl.glVertex3f(xx3/1000,yy3/1000,zz3/1000);
	        gl.glEnd();
    	}
***/
	//gl.glMatrixMode(GL2.GL_MODELVIEW);
	//gl.glFrustum(-100.0f,100.0f,-100.0f,100.0f, 1.0f, 5.0f);//FIXME remove
	//System.out.println("123> Done drawing terrain.");
	}
};
